package com.ccnu.demojava.ftp;/*
 * @author raohj
 * @date 2019/11/2718:12
 * @description add your comment
 */

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class RFtpUtil {
    static Logger log = LoggerFactory.getLogger(RFtpUtil.class);
    private static final String hostname="14.1.78.182";
    private static final int port=21;
    private static final String username="sfcg";
    private static final String password="sfcg";
    private static final String remotepath="/appfs/sfcg/files/send_broker";
    private static final String localpath="D:/tmp/TJYH/可删除";
    private static final String dateReg = "2[0123456789][0123456789][0123456789][01][123456789][0123][123456789]";

    public static void downLoad(){
        FTPClient fc = new FTPClient();
        try {
            fc.connect(hostname, port);
            fc.login(username, password);
            fc.changeWorkingDirectory(remotepath);
            fc.setFileType(FTPClient.BINARY_FILE_TYPE);
            FTPFile[] ftpFilesRemote = fc.listDirectories();
            List<FTPFile> ftpFiles = new ArrayList<>();
            for (FTPFile ftpFile : ftpFilesRemote) {
                if(ftpFile.isDirectory() && ftpFile.getName().matches(dateReg)){
                    ftpFiles.add(ftpFile);
                }
            }
            if(ftpFiles.size()<=0){
                log.info("获取远程目录失败");
                return;
            }
            ftpFiles.sort(new Comparator<FTPFile>() {
                @Override
                public int compare(FTPFile ftpFile, FTPFile t1) {
                    return t1.getName().compareTo(ftpFile.getName());
                }
            });
            FTPFile ftpFile = ftpFiles.get(0);
            // 切换远程目录
            fc.changeWorkingDirectory(remotepath+ File.separator + ftpFile.getName());
            FTPFile[] ftpFilesTBBS = fc.listFiles(ftpFile.getName());

            // 切换到本地目录
            String localPathYear = localpath + File.separator + ftpFile.getName();
            createLocalPath(localPathYear);
            // 获取远程目录文件
            for (FTPFile ftpFilesTBB : ftpFilesTBBS) {
                if(ftpFilesTBB.isFile()){
                    OutputStream os = new FileOutputStream(localPathYear + File.separator + ftpFilesTBB.getName());
                    fc.retrieveFile(ftpFilesTBB.getName(), os);
                    os.flush();
                    os.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fc.isConnected()){
                try {
                    fc.logout();
                    fc.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void createLocalPath(String createPath){
        File fileExists = new File(createPath);
        if(fileExists.exists()){
            return;
        }

        Path createDir = Paths.get(createPath);
        try {
            // 创建目录
            Files.createDirectory(createDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
